<template>
  <div id="imgPost">
    
  </div>
</template>
<script>
import bus from "../js/lib/bus";
import commons from "../common/js/common.js";
import { Toast, MessageBox, Indicator } from "mint-ui"; 

export default {
  mounted() {},
  computed: {},
  data() {
    return {};
  },
  methods: {
    submitImg() {
      this.setImgPreview("imgInp", "preview");
    },
    setImgPreview(imgObj, prev) {
      let docObj = $("#" + imgObj);
      let imgObjPreview = $("#" + prev);
      this.uploadImage(docObj, imgObjPreview);
    },
    uploadImage(docObj, imgObjPreview) {
      let imgPath = docObj.val(),
        that = this;
      if (imgPath == "") {
        return;
      }
      let strExtension = imgPath
        .substr(imgPath.lastIndexOf(".") + 1)
        .toLowerCase();
      if (
        strExtension != "jpg" &&
        strExtension != "gif" &&
        strExtension != "png" &&
        strExtension != "bmp"
      ) {
        //alert("请选择图片文件！");
        MessageBox.alert("请选择图片文件！");
        return;
      }
      Indicator.open({
        text: "上传中...",
        spinnerType: "fading-circle"
      });
      let ImgObj = new Image();
      let AllowImgFileSize = 30000000;
      ImgObj.src = docObj[0].value;

      let reader = new FileReader();
      let file = docObj[0].files[0];
      let imgUrlBase64;
      if (file) {
        imgUrlBase64 = reader.readAsDataURL(file);
        reader.onload = function(e) {
          //let result = document.getElementById("result");
          let ImgFileSize = reader.result.substring(
            reader.result.indexOf(",") + 1
          ).length;
          if (AllowImgFileSize != 0 && AllowImgFileSize < ImgFileSize) {
            MessageBox.alert("上传失败，请上传不大于30M的图片！");
            setTimeout(() => {
              Indicator.close();
            }, 500);
            return;
          }

          let $src = reader.result;
          let imgDataBase64 = $src.substring($src.indexOf(",") + 1);
          let _url = that.url;
          if (!that.url) {
            let data = {};
            data.src = $src;
            data.base64 = imgDataBase64;
            data.tranData = that.tranData || {};
            that.$http
              .post("/loansupermarket-app/personal/h5headimage", JSON.stringify( { token : commons.getCookie('token'),
              comment_url:[data.base64]})
              )
              .then(res =>{
                let resBody = res.body;
                Indicator.close();
                if(resBody.code=="000"){
                  bus.$emit("changePortrait", resBody.data);
                  sessionStorage.setItem('headImg',resBody.data);
                }else if(resBody.code=="444"){
                  //this.$router.push({ path: '/login',query:{history:1} });
                  location.href = commons.paths + "user/login.html?history=1"
                }else{
                  MessageBox.alert(resBody.mesg);
                }
              })
            setTimeout(() => {
              Indicator.close();
            }, 500);
            return;
          }
        };
        reader.onerror = function() {
          alert("error");
        };
      }
    }
  },
  activated() {}
};
</script>
<style scoped>

</style>
